<template>
  <div class="f-centerXY container">
    <div class="f-flex main">
      <div class="left" />
      <div class="right">
        <div class="message">
          <h2 class="title">梦时代后台管理平台</h2>
          <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
            <el-form-item class="message-item" prop="username">
              <span class="svg-container">
                <svg-icon icon-class="user" class-name="icon" />
              </span>
              <el-input v-model="loginForm.username" placeholder="请输入用户名" />
            </el-form-item>
            <el-form-item class="message-item" prop="password">
              <span class="svg-container">
                <svg-icon icon-class="password" class-name="icon" />
              </span>
              <el-input ref="password" v-model="loginForm.password" :type="passwordType" autocomplete="off" placeholder="请输入密码" />
              <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span>
            </el-form-item>
            <el-form-item class="message-item" prop="code">
              <span class="svg-container">
                <svg-icon icon-class="code" class-name="icon" />
              </span>
              <el-input v-model="loginForm.code" placeholder="请输入验证吗" />
              <span class="show-pwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span>
            </el-form-item>
          </el-form>
          <el-checkbox-group v-model="settings">
            <el-checkbox label="1">记住账号信息</el-checkbox>
            <!-- <el-checkbox label="2">30天免登录</el-checkbox> -->
          </el-checkbox-group>
          <el-button type="primary" round :disabled="loading" @click="handleLogin">登 录</el-button>
          <p class="tip">
            如果登录遇到问题请联系
            <a href="tel:18513300311" class="phion">18513300311</a>
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { encryption } from 'utils/encryption.js'
import { setToken, setCookies, getCookies, removeCookies } from 'utils/auth.js'
import { validUsername } from 'utils/validate.js'
import { login } from '@/api/user.js'
export default {
  name: 'Login',
  data () {
    return {
      loginForm: {
        username: '',
        password: '',
        code: '222222'
      },
      settings: [],
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  computed: {
    loginRules () {
      const validateUsername = (rule, value, callback) => {
        if (value.length > 10) {
          callback(new Error('用户名最大长度为10位'))
        } else {
          callback()
        }
      }
      const validatePassword = (rule, value, callback) => {
        if (value.length < 6) {
          callback(new Error('密码最小长度为6位'))
        } else {
          callback()
        }
      }
      const validateCode = (rule, value, callback) => {
        if (value.length < 6) {
          callback(new Error('The password can not be less than 6 digits'))
        } else {
          callback()
        }
      }

      return {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { validator: validateUsername }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { validator: validatePassword }
        ],
        code: [
          { required: true, message: '请输入验证吗', trigger: 'blur' },
          { validator: validateCode }
        ]
      }
    }

  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created () {
    let userInfo = getCookies('userInfo')
    if (userInfo) {
      userInfo = JSON.parse(getCookies('userInfo'))
      this.loginForm.username = userInfo.username
      this.loginForm.password = userInfo.password
      this.settings = ['1']
    }
  },
  methods: {
    showPwd () {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleSettings () {
      // 记住账号信息
      if (this.settings.includes('1')) {
        const data = {
          username: this.loginForm.username,
          password: this.loginForm.password
        }
        setCookies('userInfo', data, 30)
      } else {
        removeCookies('userInfo')
      }
      // 30天自动登录
      if (this.settings.includes('2')) {
        console.log(2)
      }
    },
    handleLogin () {
      this.$router.push({ path: '/' })
      // this.$refs.loginForm.validate(valid => {
      //   if (valid) {
      //     const sendData = encryption({
      //       data: this.loginForm,
      //       key: 'sirengine1234567',
      //       param: ['password']
      //     })
      //     sendData.grant_type = 'password'
      //     this.loading = true

      //     // this.loading = false
      //     // this.handleSettings()
      //     // setToken('admin')
      //     // this.$router.push({ path: this.redirect || '/' })

      //     login(sendData)
      //       .then(res => {
      //         this.loading = false
      //         this.handleSettings()
      //         setToken(res.access_token)
      //         this.$router.push({ path: this.redirect || '/' })
      //       })
      //       .catch(err => {
      //         this.loading = false
      //       })
      //   } else {
      //     console.log('error submit!!')
      //     return false
      //   }
      // })
    }
  }
}
</script>

<style lang="scss" scoped>
.message {
  ::v-deep .el-input__inner {
    padding: 0 20px;
    border-color: #fff;
    border-bottom: 1px solid;
    border-radius: 0;
  }
  ::v-deep .el-form-item.is-error {
    .el-input__inner {
      border-color: #fff;
      border-bottom-color: #f56c6c;
    }
    .icon {
      color: #f56c6c;
    }
  }
  ::v-deep .el-button.is-round {
    width: 100%;
    margin-top: 36px;
    padding: 12px 0;
  }
}
</style>

<style lang="scss" scoped>
.container {
  height: 100%;
  background: url('~images/bg_images/login.jpg') no-repeat;
  background-size: 100% 100%;
  box-shadow: 0 0 15px rgb(0 138 233 / 15%);
  .main {
    overflow: hidden;
    width: 950px;
    height: 500px;
    border-radius: 10px;
    .left {
      width: 600px;
      background: url('~images/bg_images/login_main.jpg');
      background-size: 100% 100%;
      background-color: rgba(16, 60, 95, 1);
    }
    .right {
      width: 350px;
      background: #fff;
      .message {
        padding: 0 20px;
        .title {
          margin: 26px 0;
          font-family: 'Times New Roman', Times, serif;
          font-size: 24px;
          font-weight: bolder;
          letter-spacing: 5px;
          text-align: center;
        }
        .message-item {
          position: relative;
          .svg-container {
            z-index: 1;
            display: inline-block;
            position: absolute;
            left: 0;
            top: 0;
            width: 20px;
            text-align: center;
          }
          .show-pwd {
            position: absolute;
            bottom: 0;
            right: 0;
            cursor: pointer;
          }
        }
        .tip {
          margin-top: 20px;
          color: gray;
          font-size: 12px;
          text-align: center;
          .phion {
            color: #000;
          }
        }
      }
    }
  }
}
</style>
